<template>
	<section>

		<el-col :span="24" class="toolbar"  style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-button type="primary" v-if="$store.state.role!='manager'" @click="handleAdd">添加试题</el-button>
				</el-form-item>				
				<el-form-item>
					<el-input v-model="key"  placeholder="输入查询的关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getList">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>			
		<!--列表-->

		<el-tabs v-model="tabletype"  type="card">
		    <el-tab-pane label="我的试题" name='0'></el-tab-pane>
		    <el-tab-pane label="共享试题" name='1'></el-tab-pane>
		    <el-tab-pane label="内置试题" name='2'></el-tab-pane>
		</el-tabs>	
		<!--工具条-->		
		<el-table :data="Lists" highlight-current-row v-loading="loading" style="width: 100%;" @sort-change="sortChange">
			<el-table-column  type="index"   width='80'>
			</el-table-column>
			<el-table-column prop="name" label="试题名称" width="200" >
			</el-table-column>
			<el-table-column prop="courseStage" label="试题类型" width="200" >
				<template scope="scope">
					<p>{{scope.row.courseStage=='0'?'创业基础试题':'案例分析试题'}}</p>
				</template>
			</el-table-column>	
			<el-table-column prop="type" label="题型" width="200" >
				<template scope="scope">
					<p v-if="scope.row.type==1">主观题</p>
					<p v-if="scope.row.type==2">单选题</p>
					<p v-if="scope.row.type==3">多选题</p>
					<p v-if="scope.row.type==4">判断题</p>
				</template>				
			</el-table-column>	
			<el-table-column prop="score" label="分值" width="200" >
				<template scope="scope">
					<p>{{scope.row.score}}分</p>
				</template>				
			</el-table-column>			
			<el-table-column prop="businessModelName" label="商业模式板块" width="200" >
			</el-table-column>				
			<el-table-column prop="realName" label="创建人" width="200" >
			</el-table-column>
			<el-table-column prop="orgName" label="所属单位" width="200">
			</el-table-column>			
			<el-table-column prop="createTime" label="上传时间" width="200">
			</el-table-column>
			<el-table-column prop="referenceCount" label="引用次数" width="200">
			</el-table-column>			
			<el-table-column prop="birth" min-width="300" label="操作" fixed="right" >
				<template scope="scope">
					<el-button size="small"  v-if="tabletype=='0'||$store.state.role=='admin'" type="danger" @click="handleDel(scope.row)">删除</el-button>
					<el-button size="small"  v-if="tabletype=='0'&&$store.state.role!='manager'||$store.state.role=='admin'" type="primary" @click="getone(scope.row,'edit')">编辑</el-button>
					<el-button size="small" type="success" @click="getone(scope.row,'look')">查看</el-button>
					<el-button size="small" type="warning" v-if="scope.row.isShared==false&&tabletype!=2&&$store.state.role!='admin'&&$store.state.role!='manager'" @click="goShared(scope.row,1)">共享</el-button>
					<el-button size="small" type="info" v-if="scope.row.isShared&&tabletype=='0'&&$store.state.role!='admin'"  @click="goShared(scope.row,0)">取消共享</el-button>
				</template>					
			</el-table-column>
		</el-table>
		<!--工具-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>		
		<!--新增界面-->
		<el-dialog  title="添加试题" :fullscreen="false" custom-class="workdialog" width="30%"  v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addwork" status-icon class="demo-ruleForm" :rules="addworkrule" ref="addwork" label-width="50px">		
			    <el-form-item label="试题名称"  prop="name" label-width="80">
			    	<el-input type="text" placeholder="请填写试题名称(限制15字以内)"  v-model="addwork.name" style="width: 60%;"></el-input>
			    </el-form-item>	
				<el-form-item label="试题类型"  prop="type" label-width="80px">
				    <el-radio-group v-model="addwork.courseStage">
				      <el-radio label="0">创业基础试题</el-radio>
				      <el-radio label="1">案例分析试题</el-radio>
				    </el-radio-group>
				</el-form-item>	
				<el-form-item label="题型" prop="type" >
				    <el-select placeholder="请选择题型"  v-model="addwork.type">
				      <el-option label="主观题" value="1"></el-option>
				      <el-option label="单选题" value="2" ></el-option>
				      <el-option label="多选题" value="3"></el-option>
				      <el-option label="判断题" value="4"></el-option>
				    </el-select>
				</el-form-item>	
			    <el-form-item label="题干"  prop="question">
			    	<el-input type="textarea" placeholder="限制字数50字以内" v-model="addwork.question"></el-input>
			    </el-form-item>	
			    <el-row :gutter="10" v-if="addwork.type==2">
			    	<el-col :span="12" v-for="(el,index) in optionBox">
					    <el-form-item		     	     
						    :label="el.title"
						    >					  	
						  	<el-col :span="17">	
						  		<el-input v-model="el.value"></el-input>
						  	</el-col>
						  	<el-col :span="7">
								<el-radio v-model="addwork.correctAnswer" :label="el.title" border  size="medium">正确答案</el-radio>
							</el-col>					  				    
						</el-form-item>
				    </el-col>
				</el-row>
			    <el-row :gutter="10" v-if="addwork.type==3">
			    	 <el-checkbox-group v-model="addwork.correctAnswers">
				    	<el-col :span="12" v-for="el in optionBox">
						    <el-form-item		     	     
							    :label="el.title"
							    >					  	
							  	<el-col :span="17">	
							  		<el-input v-model="el.value"></el-input>
							  	</el-col>
							  	<el-col :span="7">
									<el-checkbox  :label="el.title" border  size="medium">正确答案</el-checkbox>
								</el-col>					  				    
							</el-form-item>
					    </el-col>
				    </el-checkbox-group>
				</el-row>				
			    <el-form-item v-if="addwork.type==2||addwork.type==3">
				    <el-button type="primary" v-if="optionBox.length!='6'" @click="addDomain()">增加选项</el-button>
				      <el-button type="danger"v-if="optionBox.length!='2'" @click="removeDomain()">删除选项</el-button>
			    </el-form-item>	
			    <el-form-item v-if="addwork.type==4">
			    	    <el-radio-group v-model="addwork.correctAnswer" size="medium">
					      <el-radio-button label="1" value="">正确</el-radio-button>
					      <el-radio-button label="0" value="">错误</el-radio-button>
					    </el-radio-group>
			    </el-form-item>
				<el-form-item label="所属商业模式版块" v-if="addwork.courseStage==0" label-width="150px"  prop="bussinsmodel" >
				    <el-select placeholder="请选择版块" v-model="addwork.bussinsmodel">
				      <el-option v-for="el in bussinessList" :value="el.businessModelId" :label="el.name"></el-option>
				    </el-select>
				</el-form-item>
				<el-form-item label="分值" prop="score">
				    <el-input style="width:30%;" type="number" placeholder="灵活设分" v-model="addwork.score"></el-input>
				    <p style="color: red;">*根据题目的重要程度分为1~4分（了解），5~9分（掌握），10分（熟练）灵活设分</p>
				</el-form-item>				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--编辑界面-->
		<el-dialog  title="编辑试题" :fullscreen="false" custom-class="workdialog" width="30%"  v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editwork" status-icon class="demo-ruleForm" :rules="editworkrule" ref="editwork" label-width="50px">		
			    <el-form-item label="试题名称"  prop="name" label-width="80px">
			    	<el-input type="text" placeholder="请填写试题名称（15字以内）"  v-model="editwork.name" style="width: 60%;"></el-input>
			    </el-form-item>	
				<el-form-item label="试题类型"  prop="type" label-width="80px">
				    <el-radio-group v-model="editwork.courseStage" disabled>
				      <el-radio label="0">创业基础试题</el-radio>
				      <el-radio label="1">案例分析试题</el-radio>
				    </el-radio-group>
				</el-form-item>	
				<el-form-item label="题型" prop="type" >
				    <el-select placeholder="请选择题型" disabled   v-model="editwork.type">
				      <el-option label="主观题" value="1"></el-option>
				      <el-option label="单选题" value="2" ></el-option>
				      <el-option label="多选题" value="3"></el-option>
				      <el-option label="判断题" value="4"></el-option>
				    </el-select>
				</el-form-item>	
			    <el-form-item label="题干"  prop="question">
			    	<el-input type="textarea" placeholder="限制字数50字以内" v-model="editwork.question"></el-input>
			    </el-form-item>	
			    <el-row :gutter="10" v-if="editwork.type==2">
			    	<el-col :span="12" v-for="(el,index) in editoptionBox">
					    <el-form-item		     	     
						    :label="el.title"
						    >					  	
						  	<el-col :span="17">	
						  		<el-input v-model="el.value"></el-input>
						  	</el-col>
						  	<el-col :span="7">
								<el-radio v-model="editwork.correctAnswer" :label="el.title" border  size="medium">正确答案</el-radio>
							</el-col>					  				    
						</el-form-item>
				    </el-col>
				</el-row>
			    <el-row :gutter="10" v-if="editwork.type==3">
			    	 <el-checkbox-group v-model="editwork.correctAnswers">
				    	<el-col :span="12" v-for="el in editoptionBox">
						    <el-form-item		     	     
							    :label="el.title"
							    >					  	
							  	<el-col :span="17">	
							  		<el-input v-model="el.value"></el-input>
							  	</el-col>
							  	<el-col :span="7">
									<el-checkbox  :label="el.title" border  size="medium">正确答案</el-checkbox>
								</el-col>					  				    
							</el-form-item>
					    </el-col>
				    </el-checkbox-group>
				</el-row>				
			    <el-form-item v-if="editwork.type==2||editwork.type==3">
				    <el-button type="primary" v-if="optionBox.length!='6'" @click="editaddDomain()">增加选项</el-button>
				      <el-button type="danger"v-if="optionBox.length!='2'" @click="editremoveDomain()">删除选项</el-button>
			    </el-form-item>	
			    <el-form-item v-if="editwork.type==4">
			    	    <el-radio-group v-model="editwork.correctAnswer" size="medium">
					      <el-radio-button label="1" value="">正确</el-radio-button>
					      <el-radio-button label="0" value="">错误</el-radio-button>
					    </el-radio-group>
			    </el-form-item>
				<el-form-item label="所属商业模式版块" v-if="editwork.courseStage==0" label-width="150px"  prop="businessModelId" >
				    <el-select placeholder="请选择版块" v-model="editwork.businessModelId">
				      <el-option v-for="el in bussinessList" :value="el.businessModelId" :label="el.name"></el-option>
				    </el-select>
				</el-form-item>
				<el-form-item label="分值">
				    <el-input style="width:30%;" type="number" placeholder="分值范围1~10分之间" v-model="editwork.score"></el-input>
				     <p style="color: red;">*根据题目的重要程度分为1~4分（了解），5~9分（掌握），10分（熟练）灵活设分</p>
				</el-form-item>				 
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>		
		<!--查看界面-->
		<el-dialog  title="查看试题" :fullscreen="false" custom-class="workdialog" width="30%"  v-model="lookVisible" :close-on-click-modal="false">
			<el-form :model="editwork" status-icon class="demo-ruleForm"  ref="editwork" label-width="50px">	
			    <el-form-item label="试题名称"  prop="name" label-width="80px" >
			    	<el-input type="text" placeholder="请填写试题名称（15字以内）" disabled v-model="editwork.name" style="width: 60%;"></el-input>
			    </el-form-item>					
				<el-form-item label="试题类型"  prop="type" label-width="80px">
				    <el-radio-group v-model="editwork.courseStage" disabled>
				      <el-radio label="0">创业基础试题</el-radio>
				      <el-radio label="1">案例分析试题</el-radio>
				    </el-radio-group>
				</el-form-item>	
				<el-form-item label="题型" prop="type" >
				    <el-select placeholder="请选择版块"  v-model="editwork.type" disabled>
				      <el-option label="主观题" value="1"></el-option>
				      <el-option label="单选题" value="2" ></el-option>
				      <el-option label="多选题" value="3"></el-option>
				      <el-option label="判断题" value="4"></el-option>
				    </el-select>
				</el-form-item>	
			    <el-form-item label="题干"  prop="question">
			    	<el-input type="textarea" placeholder="限制字数50字以内" v-model="editwork.question" disabled></el-input>
			    </el-form-item>	
			    <el-row :gutter="10" v-if="editwork.type==2">
			    	<el-col :span="12" v-for="(el,index) in editoptionBox">
					    <el-form-item		     	     
						    :label="el.title"
						    >					  	
						  	<el-col :span="17">	
						  		<el-input disabled v-model="el.value"></el-input>
						  	</el-col>
						  	<el-col :span="7">
								<el-radio disabled v-model="editwork.correctAnswer" :label="el.title" border  size="medium">正确答案</el-radio>
							</el-col>					  				    
						</el-form-item>
				    </el-col>
				</el-row>
			    <el-row :gutter="10" v-if="editwork.type==3">
			    	 <el-checkbox-group v-model="editwork.correctAnswers" disabled>
				    	<el-col :span="12" v-for="el in editoptionBox">
						    <el-form-item		     	     
							    :label="el.title"
							    >					  	
							  	<el-col :span="17">	
							  		<el-input v-model="el.value" disabled></el-input>
							  	</el-col>
							  	<el-col :span="7">
									<el-checkbox disabled :label="el.title" border  size="medium">正确答案</el-checkbox>
								</el-col>					  				    
							</el-form-item>
					    </el-col>
				    </el-checkbox-group>
				</el-row>				

			    <el-form-item v-if="editwork.type==4">
			    	    <el-radio-group v-model="editwork.correctAnswer" size="medium" disabled>
					      <el-radio label="1" value="">正确</el-radio>
					      <el-radio label="0" value="">错误</el-radio>
					    </el-radio-group>
			    </el-form-item>
				<el-form-item label="所属商业模式版块" v-if="editwork.courseStage==0" label-width="150px"  prop="businessModelId" >
				    <el-select placeholder="请选择版块" disabled  v-model="editwork.businessModelId">
				      <el-option v-for="el in bussinessList" :value="el.businessModelId" :label="el.name"></el-option>
				    </el-select>
				</el-form-item>
				<el-form-item label="分值" prop="score">
				    <el-input style="width:30%;" disabled type="number" placeholder="分值范围1~10分之间" v-model="editwork.score"></el-input>
				</el-form-item>				 
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="lookVisible = false">关闭</el-button>
			</div>
		</el-dialog>			
	</section>
</template>
<script>
	import { worklist,workdelete,workadd,workone,workedit,bussinesslist,worklistbuil,worknamerule} from '../../api/api';

	//import NProgress from 'nprogress'
	export default {
		
		data() {
			var rulesname=(rules,value,callback)=>{
				let params={name:this.addwork.name};
				worknamerule(params).then((r)=>{
					if(!r){
						callback(new Error('作业名称重复'))
					}else{
						callback()
					}
				})
			}	
			var editrulesname=(rules,value,callback)=>{
				if(this.editwork.checkname!=this.editwork.name){			
					let params={name:this.editwork.name};
					worknamerule(params).then((r)=>{
						if(!r){
							callback(new Error('作业名称重复'))
						}else{
							callback()
						}
					})
				}else{
					callback()
				}
			}				
			let numberrule=(rule, value, callback)=>{
			  if (value < 0 || value > 10) {
			    callback(new Error('分值必须在 0 至 10之间'))
			  } else {
			    callback()
			  }							
			}
			let namelengthrule=(rule, value, callback)=>{
			  if (value.length > 15) {
			    callback(new Error('限制字数15字以内'))
			  } else {
			    callback()
			  }							
			}	
			let questionlengthrule=(rule, value, callback)=>{
			  if (value.length > 50) {
			    callback(new Error('限制字数50字以内'))
			  } else {
			    callback()
			  }							
			}				
			return {
				Type:"",
				addFormVisible: false,
				addLoading: false,	
				autowork:false,
				editFormVisible: false,
				editLoading: false,						
				loading: true,
				lookVisible:false,
				workupload:"work",
				Lists:[],
				fileList:[],
				checkbox:[],
				editfileList:[],	
				bussinessList:[],
				addwork:{
					name:"",
					type:"",
					courseStage:"",
					bussinsmodel:"",
					question:"",
					optionA:"",
					optionB:"",
					optionC:"",
					optionD:"",
					optionE:"",
					optionF:"",
					correctAnswer:"",
					score:"",
					correctAnswers:[]
				},
				editwork:{

				},					
				addworkrule: {
					name: [
						{ required: true, message: '请填写试题名称', trigger: 'blur' },
						{validator:rulesname,trigger: 'blur'},
						{validator:namelengthrule,trigger: 'blur'}
					],
					type:[
						{ required: true, message: '请选择试题类别', trigger: 'blur' }
					],
					question: [
						{ required: true, message: '试题题干不能为空', trigger: 'blur' },
						{validator:questionlengthrule,trigger: 'blur'}
					],					
					score:[
						{ required: true, message: '分值不能为空', trigger: 'blur' },
						{required: true, validator:numberrule, trigger: 'blur'}
					],
					bussinsmodel:[
						{ required: true, message: '请选择所属商业模式版块', trigger: 'blur' }
					]					
				},	
				editworkrule: {
					name: [
						{ required: true, message: '请填写试题名称', trigger: 'blur' },
						{validator:editrulesname,trigger: 'blur'},
						{validator:namelengthrule,trigger: 'blur'}
						
					],
					type:[
						{ required: true, message: '请选择试题类别', trigger: 'blur' }
					],
					question: [
						{ required: true, message: '试题题干不能为空', trigger: 'blur' },
						{validator:questionlengthrule,trigger: 'blur'}
					],						
					score:[
						{ required: true, message: '分值不能为空', trigger: 'blur' },
						{required: true, validator:numberrule, trigger: 'blur'}
					],
					businessModelId:[
						{ required: true, message: '请选择所属商业模式版块', trigger: 'blur' }
					]	
				},					
				optionBox:[{
						title:"A",
						value:''
					},{
						title:"B",
						value:''
				},{
						title:"C",
						value:''
				},{
						title:"D",
						value:''
				}],	
				editoptionBox:[],
				param:"",
				eidtparam:"",	
				page:"1",
				pagesize:"10",
				total:'',
				key:"",
				tabletype:'0',
				isShared:"",
				isSelf:"1",
				sign:'1'
			}
		},
		watch:{
			'tabletype':function(){
				this.pageNum='1';
				if(this.tabletype==0){
					this.isShared='';
					this.sign=1;
				}
				if(this.tabletype==1){
					this.isShared=1;
					this.sign=2;
				}	
				if(this.tabletype==2){
					this.isShared='';
					this.sign=3
				}				
				this.getList();
			}
		},
		methods: {	
		    sortChange:function(column, prop, order){
				if(column){
			      var type='';
			      if(column.prop=='createTime'){
			      	column.prop='create_time';
			      }else if(column.prop=='referenceCount'){
			      	column.prop='reference_count';
			      }
			      if(column.order=="ascending"){
			      	type=column.prop;	
			      }else if(column.order=='descending'){
			      	type=column.prop+' '+'desc';
			      }
			      this.getList(type)			 	
				}
		    },			
		    editaddDomain:function(){
		    	var title=this.editoptionBox[this.editoptionBox.length-1].title;

		        if(title=='B'){
		           title='C';
			        this.editoptionBox.push({
			          title:title,
			          value:'',
			        });		
			        return
		        }		    	
		        if(title=='C'){
		           title='D';
			        this.editoptionBox.push({
			          title:title,
			          value:'',
			        });	
			        return
		        }
		        if(title=='D'){
		           title='E';
			        this.editoptionBox.push({
			          title:title,
			          value:'',
			        });	
			        return
		        }
		        if(title=='E'){
		          title='F';
			      this.editoptionBox.push({
			          title:title,
			          value:'',
			        });	
			        return
		        }		    	
		    },	
		    editremoveDomain:function(){	 
		    	var title=this.editoptionBox[this.editoptionBox.length-1].title;
		    	var _this=this;
		    	if(title=='F'){
		    		_this.editwork.optionF=""
		    	}
		    	if(title=='E'){
		    		_this.editwork.optionE=""
		    	}
		    	if(title=='D'){
		    		_this.editwork.optionD=""
		    	}
		    	if(title=='C'){
		    		_this.editwork.optionC=""
		    	}		
		    	if(title=='B'){
					this.$message({
						message: '请保留至少两个选项',
						type: 'error'
					});		    	
					return;
		    	}			    	
		        this.editoptionBox.pop()     
		    },			
		    addDomain:function(){
		    	var title=this.optionBox[this.optionBox.length-1].title;
		        if(title=='B'){
		           title='C';
			        this.optionBox.push({
			          title:title,
			          value:'',
			        });		
			        return
		        }		    	
		        if(title=='C'){
		           title='D';
			        this.optionBox.push({
			          title:title,
			          value:'',
			        });	
			        return
		        }
		        if(title=='D'){
		           title='E';
			        this.optionBox.push({
			          title:title,
			          value:'',
			        });	
			        return
		        }
		        if(title=='E'){
		          title='F';
			      this.optionBox.push({
			          title:title,
			          value:'',
			        });	
			        return
		        }		    	
		    },	
		    removeDomain:function(){
		    	var title=this.option;
		        this.optionBox.pop()     
		    },			    
			goShared:function(row,type){
				this.$confirm('确定操作吗?', '提示', {
			        confirmButtonText: '确定',
			        cancelButtonText: '取消',					
					type: 'warning'
				}).then(() => {
					var params=new URLSearchParams();
					params.append('isShared',type);
					params.append('problemId',row.problemId);
					workedit(params).then((r) => {
						if(!r.error){
							this.$message({
								message: '操作成功',
								type: 'success'
							});
							this.getList();									
						}else{
							this.$message({
								message: '操作失败',
								type: 'error'
							});						
						}
					})	
				})				
			
			},
			handleAdd: function () {
				this.addFormVisible = true;
			},
			isSharedclick:function(type){
				this.isShared=type;
				this.getList();
			},
			//获取商业模式列表
			getbussinesslist:function(){
				bussinesslist().then((res)=>{
					$.each(res, function(i,value) {
						value.businessModelId=JSON.stringify(value.businessModelId)
					});
					this.bussinessList=res;
				})
			},	
			//获取项目列表
			getList: function (type) {
				this.loading = true;
				//NProgress.start();
				let params={pageNum:this.page,pageSize:this.pagesize,key:this.key,isShared:this.isShared,orderByClause:type,sign:this.sign};
				worklist(params).then((res) => {
					this.total=res.total;					
					this.Lists = res.list;
					this.loading = false;
				});	
			},
			getone:function(row,type){
				var params=row.problemId;
				this.editoptionBox=[];
				workone(params).then((res)=>{			
					if(!res.error){
						if(type=='edit'){
							this.editFormVisible=true;
						}else{
							this.lookVisible=true;
						}
						res.type=JSON.stringify(res.type);
						if(res.type=='3'){
							res.correctAnswers=res.correctAnswer.split(',');
						}else{
							res.correctAnswer=res.correctAnswer;
						}
						if(res.optionA!=''){
							this.editoptionBox.push({
					          title:'A',
					          value:res.optionA,
					        });	
						}
						if(res.optionB!=''){
							this.editoptionBox.push({
					          title:'B',
					          value:res.optionB,
					        });	
						}
						if(res.optionC!=''){
							this.editoptionBox.push({
					          title:'C',
					          value:res.optionC,
					        });	
						}
						if(res.optionD!=''){
							this.editoptionBox.push({
					          title:'D',
					          value:res.optionD,
					        });	
						}
						if(res.optionE!=''){
							this.editoptionBox.push({
					          title:'E',
					          value:res.optionE,
					        });	
						}		
						if(res.optionF!=''){
							this.editoptionBox.push({
					          title:'F',
					          value:res.optionF,
					        });	
						}		
						res.checkname=res.name;
						res.score=Number(res.score);
						res.courseStage=JSON.stringify(res.courseStage);				
						res.businessModelId=JSON.stringify(res.businessModelId);
						this.editwork = Object.assign({}, res);
					}
				})
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getList();
			},		            
			handleDel:function (row) {
				this.$confirm('确认删除该记录吗?', '提示', {
			        confirmButtonText: '确定',
			        cancelButtonText: '取消',					
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para =row.problemId;
					
					workdelete(para).then((res) => {
						if(!res.error){
							this.listLoading = false;
							//NProgress.done();
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getList();							
						}
					});
				})
			},
			//新增
			addSubmit: function () {			
				var pass=0;
				var _this= this;				
				if(this.addwork.type==4&&this.addwork.correctAnswer==''){
					_this.$message({
						type: 'error',
						message:'答案不能为空'		
					});							
					pass=1;
					return pass;				
				}
				if(this.addwork.type==3){
					this.addwork.correctAnswer=this.addwork.correctAnswers.join(',');
				}				
				if(this.addwork.type==2||this.addwork.type==3){

					$.each(this.optionBox, function(e,res) {
						if(res.value==""){
							_this.$message({
								type: 'error',
								message:'选项'+res.title+'不能为空'		
							});		
							pass=1;
							return pass;
						}
						if(res.title=='A'){
							_this.addwork.optionA=res.value
						}
						if(res.title=='B'){
							_this.addwork.optionB=res.value
						}
						if(res.title=='C'){
							_this.addwork.optionC=res.value
						}
						if(res.title=='D'){
							_this.addwork.optionD=res.value
						}
						if(res.title=='E'){
							_this.addwork.optionE=res.value
						}
						if(res.title=='F'){
							_this.addwork.optionF=res.value
						}						
					});		
					if(this.addwork.correctAnswer==''){
						_this.$message({
							type: 'error',
							message:'答案不能为空'		
						});		
						pass=1;
						return pass;						
					}
				}else{
					this.addwork.optionA='';
					this.addwork.optionB='';
					this.addwork.optionC='';
					this.addwork.optionD='';
					this.addwork.optionE='';
					this.addwork.optionF='';	
				}
				if(pass==1){
					return
				}
				this.param=new URLSearchParams();
				this.$refs.addwork.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {
					        confirmButtonText: '确定',
					        cancelButtonText: '取消',					
							type: 'warning'								
						}).then(() => {
							this.addLoading = true;
							this.param.append('name',this.addwork.name);
							this.param.append('type',this.addwork.type);
							this.param.append('optionA',this.addwork.optionA);
							this.param.append('optionB',this.addwork.optionB);
							this.param.append('optionC',this.addwork.optionC);
							this.param.append('optionD',this.addwork.optionD);
							this.param.append('optionE',this.addwork.optionE);
							this.param.append('optionF',this.addwork.optionF);
							this.param.append('correctAnswer',this.addwork.correctAnswer);
							this.param.append('score',this.addwork.score);
							this.param.append('question',this.addwork.question);
							this.param.append('courseStage',this.addwork.courseStage);
							if(this.addwork.courseStage==0){
								this.param.append('businessModelId',this.addwork.bussinsmodel);
							}									
							workadd(this.param).then((r) => {
								if(!r.error){
									this.addLoading = false;
									this.$message({
										message: '添加成功',
										type: 'success'
									});
									this.$refs['addwork'].resetFields();
									this.addFormVisible = false;
									this.param='';
									this.getList();		
									this.addwork={
										name:"",
										type:"",
										courseStage:"",
										bussinsmodel:"",
										question:"",
										optionA:"",
										optionB:"",
										optionC:"",
										optionD:"",
										optionE:"",
										optionF:"",
										correctAnswer:"",
										score:"",
										correctAnswers:[]
									};							
								}else{
									this.addLoading = false;
									this.param='';
									this.$message({
										message: '添加失败',
										type: 'error'
									});										
								}
							})
						}).catch(() => {
							this.addLoading = false;					
						});
					}
				});
			},
			//编辑
			editSubmit: function () {
				var pass=0;
				var _this= this;		
				if(_this.editwork.score==''){
					_this.$message({
						type: 'error',
						message:'分数不能为空'		
					});							
					pass=1;
					return pass;				
				}				
				if(this.editwork.type==4&&this.editwork.correctAnswer==''){
					_this.$message({
						type: 'error',
						message:'答案不能为空'		
					});							
					pass=1;
					return pass;				
				}				
				if(this.editwork.type==3){
					this.editwork.correctAnswer=this.editwork.correctAnswers.join(',');
				}				
				if(this.editwork.type==2||this.editwork.type==3){
					console.log(this.editoptionBox)
					$.each(this.editoptionBox, function(e,res) {
						if(res.value==""){
							_this.$message({
								type: 'error',
								message:'选项'+res.title+'不能为空'		
							});		
							pass=1;
							return pass;
						}						
						if(res.title=='A'){
							_this.editwork.optionA=res.value
						}
						if(res.title=='B'){
							_this.editwork.optionB=res.value
						}
						if(res.title=='C'){
							_this.editwork.optionC=res.value
						}
						if(res.title=='D'){
							_this.editwork.optionD=res.value
						}
						if(res.title=='E'){
							_this.editwork.optionE=res.value
						}
						if(res.title=='F'){
							_this.editwork.optionF=res.value
						}					
					});		
					if(this.editwork.correctAnswer==''){
						_this.$message({
							type: 'error',
							message:'答案不能为空'		
						});		
						pass=1;
						return pass;						
					}					
				}else{
					this.editwork.optionA='';
					this.editwork.optionB='';
					this.editwork.optionC='';
					this.editwork.optionD='';
					this.editwork.optionE='';
					this.editwork.optionF='';
					
				}
				if(pass==1){
					return
				}				
				this.editparam=new URLSearchParams();	
				this.$refs.editwork.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {
					        confirmButtonText: '确定',
					        cancelButtonText: '取消',					
							type: 'warning'								
						}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							this.editparam.append('name',this.editwork.name);
							this.editparam.append('type',this.editwork.type);
							this.editparam.append('problemId',this.editwork.problemId);
							this.editparam.append('optionA',this.editwork.optionA);
							this.editparam.append('optionB',this.editwork.optionB);
							this.editparam.append('optionC',this.editwork.optionC);
							this.editparam.append('optionD',this.editwork.optionD);
							this.editparam.append('optionE',this.editwork.optionE);
							this.editparam.append('optionF',this.editwork.optionF);
							this.editparam.append('correctAnswer',this.editwork.correctAnswer);
							this.editparam.append('score',this.editwork.score);
							this.editparam.append('question',this.editwork.question);
							this.editparam.append('courseStage',this.editwork.courseStage);
							if(this.editwork.courseStage==0){
								this.editparam.append('businessModelId',this.editwork.businessModelId);
							}							
							workedit(this.editparam).then((r) => {
								if(!r.error){
									this.editLoading = false;
									this.$message({
										message: '更新成功',
										type: 'success'
									});
									this.$refs['editwork'].resetFields();
									this.editFormVisible = false;
									this.eidtparam='',
									this.getList();									
								}else{
								    this.editLoading = false;
									this.eidtparam='',
									this.$message({
										message: '更新失败',
										type: 'error'
									});											
								}
							})
						}).catch(() => {
							this.editLoading = false;					
						})
					}
				});
			},			
		},
		mounted() {
			this.getList();
			this.getbussinesslist()
		}
	};

</script>

<style>

.workauto{
	display: none;
}
</style>